<template>
  <el-dialog v-model="visible" :title="`权限下发`" :close-on-click-modal="false" :close-on-press-escape="false" width="600">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
      label-width="100px">
      <el-form-item label="下发人员" prop="faceIds" required>
        <el-select v-model="dataForm.faceIds" placeholder="请选择" multiple filterable @change="handleChangeUser">
          <el-option v-for="item in studentList" :key="item.value" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="下发设备" prop="deviceIds" required>
        <el-select v-model="dataForm.deviceIds" placeholder="请选择" multiple>
          <el-option v-for="item in deviceList" :key="item.value" :label="item.deviceName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  faceIds: null,
  deviceIds: null,
});

const rules = ref({
  deviceId: [{ required: true, message: t("validate.required"), trigger: "change" }],
  faceId: [{ required: true, message: t("validate.required"), trigger: "change" }],
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  Promise.all([getStudentList(), getDeviceList()]).then(() => {
    if (id) {
      getInfo(id);
    }
  });
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/device/class/" + id).then((res: { data: any; }) => {
    Object.assign(dataForm, res.data);
  });
};

// 获取学生列表
const studentList = ref<any[]>([]);
const getStudentList = () => {
  baseService.get("/data/personnel/student/page", { page: 1, limit: 1000 }).then((res: { data: { list: any[]; }; }) => {
    studentList.value = res.data.list;
  });
};

// 获取设备列表
const deviceList = ref<any[]>([]);
const getDeviceList = () => {
  baseService.get("/device/info/page", {
    page: 1,
    limit: 500
  }).then((res: { data: { list: any[]; }; }) => {
    deviceList.value = res.data.list;
  });
};

const handleChangeUser = (e: any) => {
  const users = e.map((v: string) => {
    const user = studentList.value.find(d => v === d.id) || {};
    return {
      classId: user.classId,
      gender: user.gender,
      idNumber: user.idNumber,
      imagePath: user.imagePath,
      imageUrl: user.imageUrl,
      name: user.name,
      personType: user.personType,
    }
  });

  Object.assign(dataForm, {
    issAuthInfos: users,
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    baseService.post(`/device/auth/issBatch`, dataForm).then((res: any) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
